//
//  LoginViewController.m
//  Ufan
//
//  Created by yangqingluo on 15/10/17.
//  Copyright © 2015年 yangqingluo. All rights reserved.
//

#import "LoginViewController.h"


@interface LoginViewController ()

@property (strong, nonatomic) IBOutlet UITextField *usernameTextField;
@property (strong, nonatomic) IBOutlet UITextField *passwordTextField;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupBarButtonItem];
    
    _usernameTextField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _usernameTextField.layer.borderWidth = 0.8f;
//    _usernameTextField.layer.cornerRadius = 10.0f;
    
    _passwordTextField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _passwordTextField.layer.borderWidth = 0.8f;
//    _passwordTextField.layer.cornerRadius = 10.0f;
    
    
    
}

- (void)setupBarButtonItem{
    self.title = @"账号登录";
    
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 64, 44)];
    [backButton setImage:[UIImage imageNamed:@"nav_back.png"] forState:UIControlStateNormal];
    [backButton setImageEdgeInsets:UIEdgeInsetsMake(12, 0, 12, 48)];
    backButton.tag = 988;
    [backButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    [self.navigationItem setLeftBarButtonItem:backItem];
}

-(void)goBack{
//    [self.navigationController popViewControllerAnimated:YES];
    [self.navigationController dismissViewControllerAnimated:YES completion:^{}];
}

-(void)dismissKeyBoard{
    [_usernameTextField resignFirstResponder];
    [_passwordTextField resignFirstResponder];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)buttonClick:(UIButton *)sender {
    [self dismissKeyBoard];
    
    switch (sender.tag) {
            
            //登录
        case 10:{
            NSString *username = _usernameTextField.text;
            NSString *password = _passwordTextField.text;
            if (username.length == 0 || password.length == 0) {
                [self showHint:@"输入有误"];
            }
            else{
                [self showHudInView:self.view hint:@"登录中..."];
                
                [QKHttpManager loginWithUsername:username password:sha1(password) completion:^(NSObject *object, NSError *error){
                    [self hideHud];
                    
                    
                    
                    if (!error) {
                        if ([object isKindOfClass:[NSDictionary class]]) {
                            if ([((NSDictionary *)object)[@"code"] intValue] == QKHTTP_SUCCESS) {
                                [self showHint:@"登录成功"];
                                [self.navigationController dismissViewControllerAnimated:YES completion:^{
                                    
                                }];
                            }
                            else if ([((NSDictionary *)object)[@"code"] intValue] == QKHTTP_ERROR){
                                
                            }
                            
                            [self.view makeToast:[(NSDictionary *)object objectForKey:@"msg"] duration:QKToastDuration position:QKToastPoint];
                            return;
                        }
                    }
                    
                    [self.view makeToast:@"网络出错" duration:QKToastDuration position:QKToastPoint];
                    
                }];
            }
            
            
            
        }
            break;
            
        default:
            break;
    }
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
